package com.wuxu.ads_no.base.service

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.os.Process
import androidx.core.app.NotificationCompat
import com.wuxu.ads_no.util.logD

open class BaseService : Service() {
    private val mTag = "Baseservice"

    private val notification: Notification
        get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationManager: NotificationManager =
                getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            val mChannel = NotificationChannel(mTag, "服务运行于前台", NotificationManager.IMPORTANCE_HIGH)
            notificationManager.createNotificationChannel(mChannel)
            Notification.Builder(applicationContext, mTag).build()
        } else {
            val notification = NotificationCompat.Builder(this, "")
                .setContentTitle("os_framework框架")
                .setContentText("service")
                .setTicker("service正在后台运行...")
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setWhen(System.currentTimeMillis())
                .setDefaults(NotificationCompat.DEFAULT_ALL)
                .build()
            notification.flags = Notification.FLAG_AUTO_CANCEL
            notification
        }


    override fun onCreate() {
        super.onCreate()
        "onCreate".logD(mTag)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            startForeground(Process.myPid(), notification)
        }
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        onOverrideStartCommand(intent, flags, startId)
        return START_STICKY
    }

    private fun onOverrideStartCommand(intent: Intent?, flags: Int, startId: Int) {
        "OnOverride start command $intent,$flags,$startId".logD(mTag)
    }

    override fun onDestroy() {
        super.onDestroy()
        "onDestroy".logD(mTag)
    }


    override fun onBind(intent: Intent): IBinder? {
        return null
    }
}